﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace EnglishAuction.BUS
{
    public class SearchSanPham
    {
        //private static EnglishAuctionEntities db = new EnglishAuctionEntities();
        public static List<SanPham> Search(string keyword, int id, float fromPrice, float toPrice, int tg)
        {
            if (id == -1)
            {
                List<SanPham> lstAllKinds = GetTatCa(keyword).ToList();
                List<SanPham> lstTheoGia = GetTheoGia(lstAllKinds, fromPrice, toPrice).ToList();

                if (tg == 0) //khong chon thoi gian
                {
                    return lstTheoGia;
                }
                else if (tg == 3) //chon thang nay
                {
                    List<SanPham> lstTheoGiaVaTheoThang = GetTheoThang(lstTheoGia, System.DateTime.Now.Month);
                    return lstTheoGiaVaTheoThang;
                }
                else if (tg == 2) //chon tuan nay
                {
                    List<SanPham> lstTheoGiaVaTheoTuan = SearchTheoTuan(lstTheoGia, System.DateTime.Now);
                }
                else if (tg == 1) //chon hom nay
                {
                    List<SanPham> lstTheoGiaVaTheoNgay = GetTheoNgay(lstTheoGia, System.DateTime.Now.Day);
                    return lstTheoGiaVaTheoNgay;
                }
            }
            else
            {
                List<SanPham> lstOneKind = GetMotLoai(keyword, id).ToList();
                List<SanPham> lstTheoGia = GetTheoGia(lstOneKind, fromPrice, toPrice);
                if (tg == 0)
                {
                    return lstTheoGia;
                }
                else if (tg == 3) //chon thang nay
                {
                    List<SanPham> lstTheoGiaVaTheoThang = GetTheoThang(lstTheoGia, System.DateTime.Now.Month);
                    return lstTheoGiaVaTheoThang;
                }
                else if (tg == 2) //chon tuan nay
                {
                }
                else if (tg == 1) //chon hom nay
                {
                    List<SanPham> lstTheoGiaVaTheoNgay = GetTheoNgay(lstTheoGia, System.DateTime.Now.Day);
                    return lstTheoGiaVaTheoNgay;
                }
            }
            return null;
        }

        private static List<SanPham> SearchTheoTuan(List<SanPham> lst, DateTime dateTime)
        {
            throw new NotImplementedException();
        }

        private static List<SanPham> GetTheoThang(List<SanPham> lst, int month)
        {
            var kq = new List<SanPham>();

            kq = (from a in lst
                  where a.NgayLenSan.Value.Month == month
                  select a).ToList();

            return kq;
        }

        public static List<SanPham> GetTheoNgay(List<SanPham> lst, int day)
        {

            var kq = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                kq = (from a in context.SanPhams
                      where a.NgayLenSan.Value.Day == day
                      select a).ToList();
            }
            return kq;
        }

        public static List<SanPham> GetTheoGia(List<SanPham> listSP, float giatu, float giaden)
        {
            var lst = new List<SanPham>();
            lst = (from a in listSP
                   where (a.GiaSan >= giatu && a.GiaSan <= giaden)
                   select a).ToList();
            return lst;
        }

        public static List<SanPham> GetTatCa(string keyword)
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                lst = (from a in context.SanPhams
                       where a.TenSanPham.ToLower().Contains(keyword)
                                || a.MoTa.ToLower().Contains(keyword)

                       select a).ToList();
            }
            return lst;
        }

        public static List<SanPham> GetMotLoai(string keyword, int id)
        {
            List<SanPham> lst = new List<SanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                lst = (from a in context.SanPhams
                       where (a.LoaiSanPhamId == id) &&
                              (a.TenSanPham.ToLower().Contains(keyword)
                                   || a.MoTa.ToLower().Contains(keyword))

                       select a).ToList();
            }
            return lst;
        }
        public static List<LoaiSanPham> GetLoaiSanPham()
        {
            var loaisp = new List<LoaiSanPham>();
            using (var context = new EnglishAuctionEntities())
            {
                loaisp = context.LoaiSanPhams.ToList();
            }
            return loaisp;
        }

        public static List<SanPham> GetSanPham()
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.SanPhams.ToList();
            }
        }

        internal static int CountSP(List<SanPham> sanpham)
        {
            return sanpham.Count();
        }
    }
}